package com.lanou.myim.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.myim.R;

import com.lanou.myim.entity.CommentEty;
import com.lanou.myim.entity.EventBusConstant;
import com.lanou.myim.entity.IdeaEty;

import com.lanou.myim.tools.ParseSingleton;
import com.parse.ParseObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/3/17.
 */
public class DiscoveryFragmentAdapter extends BaseAdapter {
    private Context context;
    private List<ParseObject> list;
    private Perspective listener;


    public DiscoveryFragmentAdapter(Context context, List<ParseObject> list,Perspective listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        if (list != null) {
            return list.get(i);

        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        if (list != null) {
            return i;
        } else {
            return 0;
        }
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Handler handler = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_listview_discovery, null);
            handler = new Handler(view);
            view.setTag(handler);
        } else {
            handler = (Handler) view.getTag();

        }
        View viewPraise = view.findViewById(R.id.praise);
        View view1 = view.findViewById(R.id.discovery_pictures);
        if (list != null) {
            if (list.get(i).get("publishPicture")!=null){
                view1.setVisibility(View.VISIBLE);
                handler.userName.setText(list.get(i).getString("userName"));
                handler.ideaBody.setText(list.get(i).getString("ideaBody"));
                handler.time.setText(list.get(i).getString("time"));
                ParseSingleton.setPublishBitmap(list.get(i), handler.ideaPicture);
                ParseSingleton.setBitmap(list.get(i).getString("userName"), handler.userPicture);
            }else {
                view1.setVisibility(View.GONE);
                handler.userName.setText(list.get(i).getString("userName"));
                handler.ideaBody.setText(list.get(i).getString("ideaBody"));
                handler.time.setText(list.get(i).getString("time"));
                ParseSingleton.setBitmap(list.get(i).getString("userName"), handler.userPicture);
            }
            List<List<String>> comments= (List<List<String>>) list.get(i).get("comments");
            List<String> praise = (List<String>) list.get(i).get("praise");
            handler.linear.removeAllViews();
            if (praise!=null){
                String praiseBody = "";
                for (int j=0;j<praise.size();j++) {
                    viewPraise.setVisibility(View.VISIBLE);
                    praiseBody = praiseBody+praise.get(j)+",";
                }
                handler.praiseFriends.setText(praiseBody);
            }else {
                viewPraise.setVisibility(View.GONE);
            }
            if (comments!=null){
                Log.d("DiscoveryFragmentAdapte", "comments.size():" + comments.size());
                for (int j = 0; j < comments.size(); j++) {
                    Log.d("DiscoveryFragmentAdapte", "comments.get(j):" + comments.get(j));
                    TextView textView = new TextView(context);
                    textView.setText(comments.get(j).get(0) + ":" + comments.get(j).get(1));
                    handler.linear.addView(textView);
                }
            }
            // ParseSingleton.getInstance().setBitmap(ideaEty.get(i).getUserName(),handler.imageView);
//            handler.time.setText(ideaEty.get(i).getTime());
//            handler.ideaBody.setText(ideaEty.get(i).getIdeaBody());
//            handler.userName.setText(ideaEty.get(i).getUserName());
        }

        handler.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.comments(list.get(i).getString("time"), i);
            }
        });

        handler.praise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.praise(list.get(i).getString("time"),i);
            }
        });
        return view;
    }

    public void refresh(){
//        List<List<String>> refresh = new ArrayList<>();
//        refresh = (List<List<String>>) list.get(i).get("comments");
//        list.get(i).put("comments", refresh);
        notifyDataSetChanged();
    }


    class Handler {
        TextView praiseFriends;
        ImageView praise;
        ImageView comment;
        TextView userName;
        TextView ideaBody;
        TextView time;
        ImageView userPicture;
        ImageView ideaPicture;
        LinearLayout linear;
        Handler(View view) {
            linear= (LinearLayout) view.findViewById(R.id.comment_Linear);
            userName = (TextView) view.findViewById(R.id.discovery_nickName);
            ideaBody = (TextView) view.findViewById(R.id.discovery_body);
            time = (TextView) view.findViewById(R.id.discovery_time);
            userPicture = (ImageView) view.findViewById(R.id.discovery_userPicture);
            ideaPicture = (ImageView) view.findViewById(R.id.discovery_pictures);
            praise = (ImageView) view.findViewById(R.id.iv_praise);
            comment = (ImageView) view.findViewById(R.id.iv_comment);
            praiseFriends = (TextView) view.findViewById(R.id.praiseFriends);
        }
    }
    public interface Perspective{
        void comments(String time,int position);
        void praise(String time,int position);
    }
}
